package mx.kolobok.noteeditor.ui.keymap.actions;

import mx.kolobok.noteeditor.Note;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * User:  Nikita Belenkiy
 * Date: 29.05.11
 * Time: 5:08
 */
public class RenameNoteAction extends AbstractAction{


    private JList noteList;

    public RenameNoteAction(JList noteList) {
        super("Rename");
        this.noteList = noteList;
//        putValue(Action.MNEMONIC_KEY, KeyEvent.VK_D);
        putValue(Action.ACCELERATOR_KEY, Actions.RENAME_KEYSTROKE);

        this.noteList = noteList;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        renameNote();
    }
    private void renameNote(){
            Note selectedNote = (Note) noteList.getSelectedValue();
            String s = showNewTitleDialog(selectedNote);
            if (s != null && s.length() > 0) {
                selectedNote.setTitle(s);
            }
        }

    private String showNewTitleDialog(Note selectedNote) {
        return JOptionPane.showInputDialog("Note title", selectedNote.getTitle());
    }
}
